<?php echo view('header.html');?>

<body class="layui-view-bodys">
    <div class="layui-form-iframe">
        <div class="layui-row">
            <div class="layui-card-body">

                <div class="form-list-table">
                    <div class="layui-form ">

                        <div class="layui-row layui-col-space15">
                            <!--左侧 主要内容-->
                            <div class="layui-col-md9">
                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="text-red">*</span>选择分类:</label>
                                    <div class="layui-input-inline mokui-md3">
                                        <select class="" name="data[catid]" lay-verify="required">
                                            <option value="">请选择分类</option>
                                            <?php if($cats):?>
                                            <?php foreach($cats as $p):?>
                                            <option value="<?=$p['id']?>" <?= (isset($data['catid']) && $data['catid']==$p['id']) ? 'selected' :''?>><?=$p['spacer']?><?=$p['name']?></option>
                                            <?php endforeach?>
                                            <?php endif?>
                                        </select>
                                    </div>
                                    <button type="button" class="layui-btn layui-btn-sm addCategory layui-hide">添加分类</button>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="text-red">*</span>文章标题:</label>
                                    <div class="layui-input-block">
                                        <input type="text" value="<?=isset($data['title']) ? $data['title'] : ''?>" name="data[title]" lay-verify="required" placeholder="请输入文章名称" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">关键词:</label>
                                    <div class="layui-input-block">
                                        <input type="text" value="<?=isset($data['keywords']) ? $data['keywords'] : ''?>" name="data[keywords]" placeholder="关键词,号或者空格分开" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">简述:</label>
                                    <div class="layui-input-block">
                                        <textarea name="data[description]" class="layui-textarea"><?=isset($data['description']) ? $data['description'] : ''?></textarea>
                                    </div>
                                </div>

                                <div class="layui-form-item vars_file">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">封面图片:</label>
                                        <div class="layui-input-block">
                                            <div class="layui-upload file">
                                                <div class="mokui-upload-list-wrapper">
                                                    <ul class="mokui-sortable mokui-image-upload-list">

                                                        <?php if(isset($data['thumb_info']) && $data['thumb']):?>
                                                        <li class="mokui-upload-item" data-fileid="<?=$data['thumb']?>">
                                                            <img class="mokui-upload-item-thumb" alt="<?=$data['thumb_info']['filename']?>" data-src="<?=$data['thumb_info']['attachment']?>" src="<?=ams_thumb($data['thumb_info']['attachment'], 200, 200)?>">
                                                            <input type="hidden" value="<?=$data['thumb']?>" name="data[thumb]" />
                                                            <span class="mokui-upload-item-delete-span layui-icon layui-icon-close-fill mokui-upload-item-delete"></span>
                                                        </li>
                                                        <?php endif?>

                                                        <div class="mokui-upload-btn-div" id="file">
                                                            <i class="layui-icon layui-icon-addition mokui-upload-add"></i>
                                                        </div>
                                                    </ul>
                                                    <div class="mokui-upload-tips">建议尺寸 640*640，最多 1 张，单张图片不超过 2M
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <label class="layui-form-label"><span class="text-red">*</span>详情内容:</label>
                                    <div class="layui-input-block">
                                        <textarea id="content" name="data[content]"><?=isset($data['content']) ? $data['content'] : ''?></textarea>
                                    </div>
                                </div>
                            </div>


                            <!--右侧 其他内容-->
                            <div class="layui-col-md3">

                                <div class="layui-form-item" lay-filter="status">
                                    <label class="layui-form-label">状态:</label>
                                    <div class="layui-input-block">
                                        <input type="radio" name="data[status]" value="1" title="显示" checked>
                                        <input type="radio" name="data[status]" value="0" title="不显示">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">访问数:</label>
                                    <div class="layui-input-block">
                                        <input type="text" value="<?=isset($data['hits']) ? $data['hits'] : 0?>" name="data[hits]" placeholder="浏览量" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">发布时间:</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="inputtime" value="<?=isset($data['inputtime']) && $data['inputtime'] ? date('Y-m-d H:i:s',$data['inputtime']) : ''?>" name="data[inputtime]" placeholder="发布时间" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">更新时间:</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="updatetime" value="<?=isset($data['updatetime']) && $data['updatetime'] ? date('Y-m-d H:i:s',$data['updatetime']) : ''?>" name="data[updatetime]" placeholder="更新时间" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>

                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"></label>
                            <div class="layui-input-block">
                                <button class="layui-btn layui-btn-blue" lay-submit="" lay-filter="mokSubmit">提交保存</button>
                            </div>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
    </div>
    <?php echo view('footer.html');?>
    <script type="text/javascript">
        layui.use(['form', 'jquery', 'layer', 'laydate', 'layedit', 'upload'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate,
                layedit = layui.layedit,
                upload = layui.upload;

            //初始化
            //编辑器
            layedit.set({
                uploadImage: {
                    url: adminpath + 'admin/uploads/imgs',
                    type: 'post',
                    data: {
                        type: 'chart',
                    }
                }
            });
            var editindex = layedit.build('content', {
                height: 300
            });

            //获取内容
            // layedit.getContent(editindex);
            //获取纯文本
            // layedit.getText(index);
            //同步编辑器内容到textarea
            //layedit.sync(index);
            //获取编辑器选中的文本
            //layedit.getSelection(index);

            laydate.render({
                elem: '#inputtime',
                type: 'datetime'
            });

            laydate.render({
                elem: '#updatetime',
                type: 'datetime'
            });

            //监听
            $(".addCategory").on('click', function () {
                layer.open({
                    type: 2,
                    area: ['600px', '80%'],
                    content: [adminpath + 'news/admin/home/catadd']
                });
            })

            form.on('submit(mokSubmit)', function (data) {
                data.field['data[content]'] = layedit.getContent(editindex);
                $.post('', data.field, function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1000
                        }, function () {
                            // window.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 3000,
                            anim: 6
                        });
                    }
                }, 'json');
                return false;
            });



            //添加一组
            $("#add_group").on('click', function () {
                var k, i = $(".multies .layui-form-item").length;
                k = i;
                i = i + 1;
                var str = '<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">(' + i + '):</label>';
                str += '<div class="layui-input-inline"><input autocomplete="off" class="layui-input" name="data[' + k + '][name]" placeholder="名称" type="text" value="" /></div>';
                str += '<div class="layui-input-inline"><input autocomplete="off" class="layui-input" name="data[' + k + '][uri]" placeholder="URI" type="text" value="" /></div>';
                str += '<div class="layui-input-inline"><input type="checkbox" checked name="data[' + k + '][status]" value="1" title="加入验证" lay-skin="primary"></div></div></div>';
                // str += '</div></div>';
                $('.multies').append(str);
                form.render();
            });


            var uploadFile = upload.render({
                elem: '#file',
                url: adminpath + 'admin/uploads/imgs',
                accept: 'file',
                method: 'post',
                data: {
                    type: 'chart',
                },
                auto: true,
                field: 'file',
                multiple: false,
                number: 1,
                choose: function (obj) { },
                before: function (obj) {
                    layer.load();
                },
                done: function (res, index, upload) {
                    var len = $(".file .mokui-image-upload-list .mokui-upload-item").length;
                    if (len >= 1) {
                        //删除
                        var fid = res.data.fileid;
                        console.log(fid);
                        ams_del_attach(fid);
                        layer.msg('只能上传一个文件', {
                            time: 1500,
                            anim: 6
                        });
                    } else if (res.code == '0' && len <= 0) {
                        var str = '';
                        str += '<li class="mokui-upload-item" data-fileid="' + res.data.fileid + '"><img class="mokui-upload-item-thumb" alt="' + res.data.title + '" data-src="' + res.data.src + '" src="' + res.data.thumb + '">';
                        str += '<input type="hidden" value="' + res.data.fileid + '" name="data[thumb]" />';
                        str += '<span class="mokui-upload-item-delete-span layui-icon layui-icon-close-fill mokui-upload-item-delete"></span></li>';
                        $(".file .mokui-image-upload-list .mokui-upload-btn-div").before(str);
                    } else {
                        layer.msg(res.msg, {
                            time: 1500,
                            anim: 6
                        });
                    }
                    console.log(res, index);
                    layer.closeAll('loading');
                },
                error: function (index, upload) {
                    layer.closeAll('loading');
                }
            });

        });
    </script>
</body>

</html>
